#ifndef PLANWIDGET_H
#define PLANWIDGET_H

#include "FramelessWindow.h"
#include "TaskItemWidget.h"
#include "plan/PlanTask.h"

#include <QButtonGroup>
#include <QComboBox>
#include <QDate>
#include <QDateEdit>
#include <QEvent>
#include <QLabel>
#include <QLineEdit>
#include <QPushButton>
#include <QScrollArea>
#include <QString>
#include <QTimer>
#include <QVBoxLayout>

/**
 * @brief 计划任务列表窗口（支持独立/嵌入两种模式）
 */
class PlanWidget : public FramelessWindow
{
    Q_OBJECT

public:
    explicit PlanWidget(QWidget* parent = nullptr);
    ~PlanWidget();

    static constexpr int TITLE_BAR_HEIGHT = 48;

public slots:
    void refreshTasks();

public:
    /**
     * @brief [FDD-2025-03-015][AC-001] 设为紧凑模式（嵌入主界面）
     */
    void setCompactMode(bool compact);

protected:
    bool eventFilter(QObject* watched, QEvent* event) override;
    void changeEvent(QEvent* event) override;

private:
    void configureWindowFrame();
    void setupUi();
    void setupConnections();
    void setupTitleBar(QVBoxLayout* frameLayout);
    void applyDefaultStyle();
    void updateFrameAppearance();
    void toggleMaximizeState();
    void updateMaximizeButtonIcon();
    void loadTasks(const QDate& date);
    void loadWeekTasks();
    void clearTaskList();
    void addTaskWidget(const PlanTask& task);
    QString getWeekDateRange() const;
    int indexForDropPosition(const QPoint& pos) const;
    void updateDropIndicator(int index);
    void hideDropIndicator();
    bool matchTask(const PlanTask& task) const;

private slots:
    void onDateChanged(const QDate& date);
    void onViewModeChanged(int id);
    void onTodayClicked();
    void onQuickAddTask();
    void onAddTaskClicked();
    void onRefreshClicked();
    void onFilterChanged(int index);
    void onSearchChanged(const QString& text);
    void onExportClicked();
    void onImportClicked();

    void onTaskStatusToggled(const QString& taskId, bool completed);
    void onTaskEditRequested(const PlanTask& task);
    void onTaskDeleteRequested(const QString& taskId);
    void onTaskTimerRequested(const PlanTask& task);

    void onTasksUpdated(const QDate& date);
    void onTaskReminderTriggered(const PlanTask& task);
    void onTaskCompleted(const PlanTask& task);

private:
    enum ViewMode {
        DayView = 0,
        WeekView = 1
    };

    ViewMode m_viewMode;
    QDate m_currentDate;

    QDateEdit* m_dateEdit;
    QPushButton* m_todayBtn;
    QLineEdit* m_quickAddEdit;
    QPushButton* m_addTaskBtn;
    QPushButton* m_refreshBtn;
    QPushButton* m_exportBtn;
    QPushButton* m_importBtn;
    QComboBox* m_filterCombo;
    QLineEdit* m_searchEdit;
    QPushButton* m_dayViewBtn;
    QPushButton* m_weekViewBtn;
    QButtonGroup* m_viewButtonGroup;
    QLabel* m_dateRangeLabel;
    QLabel* m_taskCountLabel;

    QWidget* m_frameWidget = nullptr;
    QWidget* m_titleWidget = nullptr;
    QWidget* m_mainWidget = nullptr;
    QLabel* m_titleIcon = nullptr;
    QLabel* m_titleLabel = nullptr;
    QPushButton* m_minButton = nullptr;
    QPushButton* m_maxButton = nullptr;
    QPushButton* m_closeButton = nullptr;
    QVBoxLayout* m_contentLayout = nullptr;

    QScrollArea* m_scrollArea;
    QWidget* m_taskContainer;
    QVBoxLayout* m_taskLayout;

    QLabel* m_emptyLabel;
    QList<TaskItemWidget*> m_taskWidgets;

    QWidget* m_dropIndicator = nullptr;
    int m_dropIndicatorIndex = -1;

    enum class FilterMode { All=0, TodoOnly=1, CompletedOnly=2 };
    FilterMode m_filterMode = FilterMode::All;
    QString m_searchKeyword;

    bool m_compactMode = false;
    QString m_defaultStyleSheet;
};

#endif // PLANWIDGET_H
